package com.limh.blehelper.service;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.text.TextUtils;

import com.limh.blehelper.Ble2Manager;
import com.limh.blehelper.event.ScanEvent;
import com.limh.blehelper.utils.BleLog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BlueScanningService extends Service {
    /**
     * 扫描到的蓝牙列表
     */
    private List<BluetoothDevice> scaningList;
    private Set<String> temp;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(broadcastReceiver, filter);

        if (null == scaningList) {
            scaningList = new ArrayList<>();
        }
        if (null == temp) {
            temp = new HashSet<>();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        BleLog.d("onStartCommand");
        if (null != Ble2Manager.getInstance().bluetoothAdapter && !Ble2Manager.getInstance().bluetoothAdapter.isDiscovering()) {
            scaningList.clear();
            temp.clear();
            EventBus.getDefault().post(new ScanEvent(ScanEvent.SCAN_START));
            Ble2Manager.getInstance().bluetoothAdapter.startDiscovery();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != broadcastReceiver) {
            unregisterReceiver(broadcastReceiver);
        }
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!TextUtils.isEmpty(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                switch (action) {
                    case BluetoothDevice.ACTION_FOUND:
                        if (null != device) {
                            EventBus.getDefault().post(new ScanEvent(ScanEvent.SCAN_FOUND, device));
                            if (temp.contains(device.getAddress())) {
                                return;
                            }
                            temp.add(device.getAddress());
                            scaningList.add(device);
                        }
                        break;
                    case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                        BleLog.e("scanning finish");
                        EventBus.getDefault().post(new ScanEvent(ScanEvent.SCAN_FINISH, scaningList));
                        stopSelf();
                        break;
                }
            }
        }
    };
}

